package com.tz.colawallet.basecode.comm;

import android.content.Context;

import com.tz.colawallet.basecode.util.SPUtils;
import com.tz.colawallet.constant.Key;
import com.tz.colawallet.service.ApiService;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by chenjian on
 */
public class RetrofitHelpNew {
    //测试服
//    public final static String NEW_BASE_URL = "http://47.101.199.31/";
    public final static String NEW_BASE_URL = "http://www.bsdaidaizhu.com/";
//    public final static String NEW_BASE_URL = "http://61.160.248.135:2666/";

    public static final int DEFAULT_TIMEOUT = 30;


    private Retrofit retrofit;

    private static ApiService openApi;

    private static ApiService secretApi;

    /**
     * 正常访问
     */
    private RetrofitHelpNew(final Context context) {
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));//打印其请求和返回
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        httpClientBuilder.retryOnConnectionFailure(true);
        httpClientBuilder.addNetworkInterceptor(new CaheInterceptor(context));

        httpClientBuilder.addNetworkInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
//                        .addHeader("Content-Type", "text/html;application/xhtml+xml;application/xml;charset=utf-8")
//                        .addHeader("Content-Type", "application/json;charset=UTF-8")
//                        .addHeader("Accept-Encoding", "gzip,deflate")
//                        .addHeader("Accept-Language", "zh-CN,zh")
//                        .addHeader("Connection", "keep-alive")
                        .addHeader("token", (String) SPUtils.get(context, Key.TOKEN, ""))
                        .build();
                return chain.proceed(request);
            }
        });

        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(NEW_BASE_URL)
                .build();
    }


    //不需要登录
    public static ApiService getOpenApi(Context context) {
        if (openApi == null) {
            openApi = new RetrofitHelpNew(context).retrofit.create(ApiService.class);
        }
        return openApi;
    }

    //登录后
    public static ApiService getSecretApi(Context context) {
        if (secretApi == null) {
            secretApi = new RetrofitHelpNew(context).retrofit.create(ApiService.class);
        }
        return secretApi;
    }


    public static void logout() {
        secretApi = null;
    }
}
